<?php
/* 
 * Project: oCoder Framework
 * File: /controllers/user.php
 * Purpose: controller for the user of the app.
 * Author: Trung Hoang
 */

class DiaDiemDuLichController extends BaseController
{
    //add to the parent constructor
    public function __construct($action, $urlValues) {
       // Check login
        // if(!Helper::isLogin()){
        //     //save current url: after login, auto redirect client to current url
        //     $_SESSION['lastUrl'] = Helper::currentPageURL();
        //     //redirect to login page
        //     header( 'Location: '.BASE_URL.'account/login' ) ;
        // }
         if(!Helper::isLogin()){
            //save current url: after login, auto redirect client to current url
            $_SESSION['lastUrl'] = Helper::currentPageURL();
            //redirect to login page
            header( 'Location: '.BASE_URL.'account/login' ) ;
        }
        parent::__construct($action, $urlValues);
        
        //create the model object
        require("models/diadiemdulich.php");
        $this->model = new DiaDiemDuLichModel();      
    }

    //default action
    protected function index()
    {
        if(Helper::Authenticate('XemDiaDiemDuLich'))
        {
            if(!empty($_GET['id']))
            {
                 $page = $_GET['id'];
            }
            else
            {
                $page = 1;
            }           
            $this->view->output_admin($this->model->index($page)); 
        }
         else Helper::redirect();
    }
     protected function place()
    {                
        if(!empty($_GET['id']))
        {
             $alias = $_GET['id'];             
        }
        else
        {
            $alias = 'cau-truong-tien';
        }    
        if(!isset($_SESSION['view_place']) or $_SESSION['view_place']!=$alias)          
        {       
            $result = $this->model->update_view_place($alias);       
            $_SESSION['view_place'] = $alias;

        } 
        $this->view->output($this->model->place($alias)); 
    }
    protected function province()
    {                
        if(!empty($_GET['id']))
        {
             $alias = $_GET['id'];   
        }
        else
        {
            $alias = 'thua-thien-hue';
        }    
        if(!isset($_SESSION['view_province']) or $_SESSION['view_province']!=$alias)          
        {       
            $result = $this->model->update_view_province($alias);
            $_SESSION['view_province'] = $alias;

        }       
        
        $this->view->output($this->model->province($alias)); 
    }
     protected function tracuu()
    {             
        if ( !empty($_POST)) {
            $kinhdo = $_POST['kinhdo'];
            $vido = $_POST['vido'];
        }
        else
        {
            $kinhdo ='';
            $vido ='';
        }
        $this->view->tracuu($this->model->tracuu($kinhdo, $vido)); 
    }
     protected function search()
    {   
        if(isset($_POST['keyword'])) {                   
            if(!empty($_POST['keyword']))
            {
                 $keyword = trim(preg_replace('/<script\b[^>]*>(.*?)<\/script>/is', "", $_POST['keyword']));                                 
                 if(strlen($keyword)==0){
                    header("Location: tracuu");
                 }
                 $keyword_array = explode(" ", $keyword);
            }
            else
            {
                header("Location: tracuu");
            } 
        }   
        else {
            header("Location: tracuu");
        }
        $where_search = '';
        foreach ($keyword_array as $keyword1) {
            $where_search = $where_search.'TenDiem like "%'.$keyword1.'%" and ';
        }
        $where_search = substr($where_search, 0, strlen($where_search)-5);
        $this->view->output($this->model->search($where_search, $keyword)); 
    }
    //create action
    protected function create()
    {
        if(Helper::Authenticate('ThemDiaDiem'))
        {
            //load data post from client
            if ( !empty($_POST)) {
                //save item and return id of item created
                if (empty($_POST['alias'])){
                    $_POST['alias'] = Helper::ToAlias($_POST['tendiem']);               
                }
                $tukhoadiadiem = Helper::ToAlias($_POST['tendiem']);
                $data1 = array(
                    'TenDiem' => $_POST['tendiem'], 
                    'Alias' => $_POST['alias'], 
                    'MoTa' => $_POST['mota'],
                    'TinhThanh' => $_POST['tinhthanh'], 
                    'LoaiDiaDiem' => $_POST['loaidiadiem'], 
                    'DiaChi' => $_POST['diachi'],
                    'KinhDo' => $_POST['kinhdo'], 
                    'ViDo' => $_POST['vido'], 
                    'HinhAnh' => $_POST['hinhanh'], 
                    'TuKhoaDiaDiem' => $tukhoadiadiem,
                    'TinhTrang' => $_POST['tinhtrang'], 
                    );
                for ($i=1; $i <= MAX_IMAGE ; $i++) { 
                   if(!empty($_POST['hinhanh'.$i]))
                   {
                        $data2 = array(
                            'TuKhoaDiaDiem' => $tukhoadiadiem, 
                            'MoTa' =>  $_POST['description'.$i], 
                            'DuongDan' => $_POST['hinhanh'.$i], 
                            'TinhTrang' => 'Hoạt động',
                            'Loai' => 'Hình ảnh',                        
                        );
                        $result1 = $this->model->add_media($data2);
                   }
                }
                for ($i=1; $i <= MAX_VIDEO ; $i++) { 
                   if(!empty($_POST['video'.$i]))
                   {
                        $data3 = array(
                            'TuKhoaDiaDiem' => $tukhoadiadiem, 
                            'MoTa' =>  $_POST['video_description'.$i], 
                            'DuongDan' => $_POST['video'.$i], 
                            'TinhTrang' => 'Hoạt động',
                            'Loai' => 'Video',                        
                        );
                        $result1 = $this->model->add_media($data3);
                   }
                }
                $result = $this->model->create($data1);
                if($result){
                    $alert = array(
                        'class' => 'alert-success',
                        'message' => 'Thêm địa điểm thành công' 
                        );
                    $_SESSION['message'] = $alert;
                    header( 'Location: '. BASE_URL . 'diadiemdulich/index' ) ;
                }
            }
            //load form create
            $this->view->output_admin($this->model->create('')); 
        }
         else Helper::redirect();
    }

    //update action
    protected function update(){
        if(Helper::Authenticate('SuaDiaDiemDuLich'))
        {
            //load data update post from client
            if(!empty($_POST)){
                //get data from POST
                $id = $_POST['id'];
                if (empty($_POST['alias'])){
                    $_POST['alias'] = Helper::ToAlias($_POST['tendiem']);               
                }
                $data1 = array(
                    'TenDiem' => $_POST['tendiem'], 
                    'Alias' => $_POST['alias'], 
                    'MoTa' => $_POST['mota'],
                    'TinhThanh' => $_POST['tinhthanh'], 
                    'LoaiDiaDiem' => $_POST['loaidiadiem'], 
                    'DiaChi' => $_POST['diachi'],
                    'KinhDo' => $_POST['kinhdo'], 
                    'ViDo' => $_POST['vido'], 
                    'HinhAnh' => $_POST['hinhanh'], 
                    'TinhTrang' => $_POST['tinhtrang'], 
                    );
               
                for ($i=1; $i <= MAX_IMAGE ; $i++) { 
                   if(!empty($_POST['hinhanh'.$i]))
                   {
                        $data2 = array(
                            'TuKhoaDiaDiem' => $_POST['tukhoadiadiem'], 
                            'MoTa' =>  $_POST['description'.$i], 
                            'DuongDan' => $_POST['hinhanh'.$i], 
                            'TinhTrang' => 'Hoạt động',
                            'Loai' => 'Hình ảnh',                        
                        );
                        $result1 = $this->model->add_media($data2);
                   }
                }
                for ($i=1; $i <= MAX_VIDEO ; $i++) { 
                   if(!empty($_POST['video'.$i]))
                   {
                        $data3 = array(
                            'TuKhoaDiaDiem' => $_POST['tukhoadiadiem'], 
                            'MoTa' =>  $_POST['video_description'.$i], 
                            'DuongDan' => $_POST['video'.$i], 
                            'TinhTrang' => 'Hoạt động',
                            'Loai' => 'Video',                        
                        );
                        $result1 = $this->model->add_media($data3);
                   }
                }
                //update item -> return total rows affected
                $result = $this->model->edit($id, $data1);
                if($result){
                    $alert = array(
                        'class' => 'alert-success',
                        'message' => 'Cập nhật thông tin địa điểm thành công' 
                        );
                    $_SESSION['message'] = $alert;
                    header( 'Location: '. BASE_URL . 'diadiemdulich/index' ) ;
                }
            }
            //load form update
            $this->view->output_admin($this->model->edit($id=$this->urlValues['id'], $data=''));
        }
         else Helper::redirect();
    }

    //delete action
    protected function delete()
    {
        if(Helper::Authenticate('XoaDiaDiemDuLich'))
        {
            //when client delete multi item
            if(!empty($_POST)){
                $result = $this->model->delete('dd_diadiemdulich', 'id IN('.implode(',',$_POST['id']).')');
            }else{
                $result = $this->model->delete('dd_diadiemdulich', 'id='.$this->urlValues['id']);
            }

            if($result){
                $alert = array(
                    'class' => 'alert-success',
                    'message' => 'Đã xóa địa điểm' 
                    );
                $_SESSION['message'] = $alert;
            }

            header( 'Location: '. BASE_URL . 'diadiemdulich/index' ) ;
        }
         else Helper::redirect();
    }

    protected function deleteimage()
    {
        if(Helper::Authenticate('XoaAnhDiaDiemDuLich'))
        {
            //when client delete multi item
            if(!empty($_POST)){
                $result = $this->model->delete('dd_media', 'id IN('.implode(',',$_POST['id']).')');
            }else{
                $result = $this->model->delete('dd_media', 'id='.$this->urlValues['id']);
            }

            if($result){
                $alert = array(
                    'class' => 'alert-success',
                    'message' => 'Đã xóa hình ảnh' 
                    );
                $_SESSION['message'] = $alert;
            }

            header( 'Location: '. BASE_URL . 'diadiemdulich/index' ) ;
        }
         else Helper::redirect();
    }
}

?>